package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.PayLog;
import cn.wolfcode.feign.PayFeignApi;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.Map;

/**
 * @author LuoRis
 */
@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private PayFeignApi payFeignApi;
    @Value(value = "${pay.frontEndPayUrl}")
    private String frontEndPayUrl;


    /**
     * "立即支付" 接口
     *
     * @param orderNo
     * @param type
     * @return
     */
    @RequestMapping("/alipay")
    public Result<String> alipay(String orderNo, int type) {
        if (StringUtils.isEmpty(orderNo) && StringUtils.isEmpty(type)) {
            throw new BusinessException(SeckillCodeMsg.PARAMETER_ERROR);
        }
        String payJsp = "";
        if (type == PayLog.PAY_TYPE_ONLINE) {
            //RMB支付
            payJsp = orderInfoService.payOnline(orderNo);
        } else if (type == PayLog.PAY_TYPE_INTERGRAL) {
            //积分支付
            payJsp = orderInfoService.payIntegral(orderNo);
        }else {
            payJsp="";
        }

        return Result.success(payJsp);
    }

    /**
     * 同步回调
     * @param response
     * @param params
     * @throws IOException
     */
    @RequestMapping("/return_url")
    public void returnUrl(HttpServletResponse response,@RequestParam Map<String, String> params) throws IOException {
        //调用SDK验证签名
        boolean signVerified = payFeignApi.rsaCheckV(params).getData();
        if(signVerified) {
            response.sendRedirect(frontEndPayUrl+params.get("out_trade_no"));
        }else {
            response.sendRedirect("验签失败");
        }
    }

    /**
     * 异步回调
     * @param params
     * @param response
     * @throws IOException
     */
    @RequestMapping("/notify_url")
    public void notifyUrl(@RequestParam Map<String, String> params,HttpServletResponse response) throws IOException {

        boolean signVerified = payFeignApi.rsaCheckV(params).getData();
        //验证成功
        String orderNo = params.get("out_trade_no");
        if (StringUtils.isEmpty(orderNo)){
            throw new BusinessException(SeckillCodeMsg.PARAMETER_ERROR);
        }
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        if (StringUtils.isEmpty(orderInfo)){
            throw new BusinessException(SeckillCodeMsg.PARAMETER_ERROR);
        }
        if(signVerified) {
            if(orderInfo.getStatus().equals(OrderInfo.STATUS_ARREARAGE)){
                orderInfoService.changePayStatus(orderNo,OrderInfo.STATUS_ACCOUNT_PAID);
            }else if (orderInfo.getStatus().equals(OrderInfo.STATUS_ACCOUNT_PAID)){
                throw new BusinessException(SeckillCodeMsg.ALREADY_PAY);
            }

            response.sendRedirect("success");

        }else {//验证失败
            response.sendRedirect("fail");
        }
    }

    /**
     * 退款
     * @param orderNo
     * @return
     */
    @RequestMapping("/refund")
    public Result<String> refund(String orderNo){
        if (StringUtils.isEmpty(orderNo)){
            throw new BusinessException(SeckillCodeMsg.REFUND_ERROR);
        }
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        if (StringUtils.isEmpty(orderInfo)){
            throw new BusinessException(SeckillCodeMsg.REFUND_ERROR);
        }
        int payType = orderInfo.getPayType();

        if (StringUtils.isEmpty(payType)){
            throw new BusinessException(SeckillCodeMsg.REFUND_ERROR);
        }

        if (payType==OrderInfo.PAYTYPE_INTERGRAL){
            orderInfoService.refundIntegral(orderNo);
        }else if (payType==OrderInfo.PAYTYPE_ONLINE){
            orderInfoService.refund(orderNo);
        }


        return Result.success("");

    }
}
